/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: CrawlPriceOfAutoHome.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.price.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年7月29日 下午4:49:42 
 * @version: V1.0   
 */
package com.zizo.autohome.price.crawl;

import java.io.File;
import java.sql.Connection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.price.bean.AutoHomePrice;
import com.zizo.autohome.price.dao.PriceDaoOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: CrawlPriceOfAutoHome
 * @Description:
 * @author: Alexis
 * @date: 2016年7月29日 下午4:49:42
 */
public class PriceCrawlOfAutoHomeByFile extends Crawl {

	PriceDaoOfAutoHome pricedao = (PriceDaoOfAutoHome) dao;

	/**
	 * @Title:PriceCrawlOfAutoHome
	 * @Description:
	 * @param id
	 * @param dao
	 */
	public PriceCrawlOfAutoHomeByFile(String id, DAO dao) {
		super(id, dao);
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		String page = Util.getMatched(doc.select(selectStr).text(), "(.*共)(\\d{1,})(.*)").group(2);
		return Integer.parseInt(page);
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String pathname) {
		// pathname = /mnt/mirror/autohome/price/...html
		Document doc = DocUtil.getDocByFile(pathname);
		Elements lis = doc.select("li.price-item");
		for (Element li : lis) {
			String link = li.select("li.head-item div.txcon a").attr("href");
			int price_id = Integer.parseInt(li.attr("data-pkid"));
			int price_ppid = getPPID(link);// 品牌ID
			int price_cxid = Integer.parseInt(Util.getMatched(link, "(.*p-)(\\d{1,})(#.*)").group(2));// 车型ID
			String user = li.select("div.user-name a.uname").first().attr("href");
			System.out.println(li.select("div.user-name a.uname").first().text());
			int price_yhid = Integer.parseInt(Util.getMatched(user, "(.*cn/)(\\d{1,})(#.*)").group(2));// 用户ID
			int price_rz = li.select("i.icon16-vorange").size() > 0 ? 1 : 0;// 是否为认证车主
			String price_fbsj = li.select("span.fn-desc").text().split(" 发表")[0];// 发表时间
			double price_lcj = 0;// 裸车价
			double price_zdj = 0;// 指导价
			double price_gzs = 0;// 购置税
			double price_sybx = 0;// 商业保险
			double price_hjjg = 0;// 合计价格
			double price_spfy = 0;// 上牌费用
			double price_ccsy = 0;// 车船使用税
			double price_jqx = 0;// 交强险
			String price_cxtc = "";// 促销套餐
			String price_gcsj = "1900-01-01";// 购车时间
			String price_gcsf = "";// 购车省份
			String price_gcds = "";// 购车地市
			int price_gmsj = 0;// 购买商家
			String price_gmpj = "";// 购买评价
			String price_gmgs = "";// 购买感受
			double price_lspz = 0;// 临时牌照
			double price_dkfy = 0;// 贷款费用
			double price_dksxf = 0;// 贷款手续费
			double price_yb = 0;// 延保
			double price_fqsx = 0;// 分期手续
			double price_dj = 0;// 定金
			double price_fwf = 0;// 服务费
			double price_ajfy = 0;// 按揭费用
			double price_ajlx = 0;// 按揭利息
			double price_xh = 0;// 选号
			double price_zs = 0;// 装饰
			double price_zsm = 0;// 装饰膜
			double price_wlyq = 0;// 5路1桥
			Elements items = li.select("div.price-item-bd li:not(.head-item)");
			for (Element item : items) {
				String name = item.select("div.name").text();
				switch (name) {
				case "裸车价：":
					price_lcj = getstandardFormat(item.select("span.price-desc").text());// 裸车价
					break;
				case "指导价：":
					price_zdj = getstandardFormat(item.select("div.txcon").text());// 指导价：
					break;
				case "购置税：":
					price_gzs = getstandardFormat(item.select("div.txcon").text());// 购置税：
					break;
				case "商业保险：":
					price_sybx = getstandardFormat(item.select("div.txcon").text());// 商业保险：
					break;
				case "合计价格：":
					price_hjjg = getstandardFormat(item.select("span.price-desc").text());// 合计价格：
					break;
				case "上牌费用：":
					price_spfy = getstandardFormat(item.select("div.txcon").text());// 上牌费用：
					break;
				case "车船使用税：":
					price_ccsy = getstandardFormat(item.select("div.txcon").text());// 车船使用税：
					break;
				case "交强险：":
					price_jqx = getstandardFormat(item.select("div.txcon").text());// 交强险：
					break;
				case "临时牌照：":
					price_lspz = getstandardFormat(item.select("div.txcon").text());// 临时牌照：
					break;
				case "贷款费用：":
					price_dkfy = getstandardFormat(item.select("div.txcon").text());// 贷款费用：
					break;
				case "贷款手续费：":
					price_dksxf = getstandardFormat(item.select("div.txcon").text());// 贷款手续费：
					break;
				case "延保：":
					price_yb = getstandardFormat(item.select("div.txcon").text());// 延保：
					break;
				case "分期手续费：":
					price_fqsx = getstandardFormat(item.select("div.txcon").text());// 分期手续费：
					break;
				case "定金：":
					price_dj = getstandardFormat(item.select("div.txcon").text());// 定金：
					break;
				case "服务费：":
					price_fwf = getstandardFormat(item.select("div.txcon").text());// 服务费：
					break;
				case "按揭费用：":
					price_ajfy = getstandardFormat(item.select("div.txcon").text());// 按揭费用：
					break;
				case "按揭利息：":
					price_ajlx = getstandardFormat(item.select("div.txcon").text());// 按揭利息：
					break;
				case "选号：":
					price_xh = getstandardFormat(item.select("div.txcon").text());// 选号
					break;
				case "装饰：":
					price_zs = getstandardFormat(item.select("div.txcon").text());// 装饰
					break;
				case "装饰膜等：":
					price_zsm = getstandardFormat(item.select("div.txcon").text());// 装饰膜等
					break;
				case "5路1桥：":
					price_wlyq = getstandardFormat(item.select("div.txcon").text());// 5路1桥：
					break;
				case "促销套餐：":
					price_cxtc = item.select("div.txcon").text();// 促销套餐
					break;
				case "购车时间：":
					price_gcsj = item.select("div.txcon").text();// 购车时间
					break;
				case "购车地点：":
					price_gcsf = item.select("div.txcon").attr("pid");// 省份
					price_gcds = item.select("div.txcon").attr("cid");// 地市
					break;
				case "购买商家：":
					if (item.select("div.info").size() > 0
							&& item.select("div.info p").first().select("a").size() > 0) {
						price_gmsj = Integer.parseInt(
								Util.getMatched(item.select("div.info p").first().select("a").first().attr("href"),
										"(.*cn/)(\\d{1,})(#.*)").group(2));// 购买商家
					}
					price_gmpj = item.select("span.level").text();// 购买评价
					break;
				case "购买感受：":
					price_gmgs = item.select("div.txcon").text();// 5路1桥：
					break;
				default:
					break;
				}
			}
			String price_gxsj = Util.getCurrentDateTime();// 更新时间
			AutoHomePrice price = new AutoHomePrice(price_id, price_ppid, price_cxid, price_yhid, price_rz, price_fbsj,
					price_lcj, price_zdj, price_gzs, price_sybx, price_hjjg, price_spfy, price_ccsy, price_jqx,
					price_lspz, price_dkfy, price_dksxf, price_yb, price_fqsx, price_dj, price_fwf, price_ajfy,
					price_ajlx, price_xh, price_zs, price_zsm, price_wlyq, price_cxtc, price_gcsj, price_gcsf,
					price_gcds, price_gmsj, price_gmpj, price_gmgs, price_gxsj);
			pricedao.insertData(price);
			System.out.println(price_id);
		}
	}

	/**
	 * @Title: getstandardFormat
	 * @Description:
	 * @param data
	 * @return 数据格式化
	 * @return: double
	 * @author: Alexis
	 */
	public double getstandardFormat(String data) {
		data = data.replaceAll(",", "");
		double res = 0;
		if (data.contains("未填写")) {
			return 0;
		} else if (data.contains("元")) {
			res = Double.parseDouble(data.split("元")[0]) / 10000.0;
		} else {
			res = Double.parseDouble(data.split("万")[0]);
		}
		return res;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */
	@Override
	public void crawlContent(String url) {

	}

	/**
	 * @Title: getPPID
	 * @Description: 得到品牌的ID
	 * @param url
	 * @return
	 * @return: int
	 * @author: Alexis
	 */
	public int getPPID(String url) {
		Document doc = DocUtil.getDoc(url);
		int ppid = Integer.parseInt(doc.select("div.subnav-title-return a").attr("href").split("/")[3]);
		return ppid;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		getItems(this.id);
	}

	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection("price", "localhost", "3306");
		PriceDaoOfAutoHome dao = new PriceDaoOfAutoHome(currConn);
		ExecutorService fix = Executors.newFixedThreadPool(30);
		File[] files = new File("/mnt/mirror/autohome/price/").listFiles();
		for (File file : files) {
			PriceCrawlOfAutoHomeByFile price = new PriceCrawlOfAutoHomeByFile(file.getAbsolutePath(), dao);
			fix.execute(price);
		}
		fix.shutdown();
	}
}
